package cn.echo.controller;

import cn.echo.entity.User;
import cn.echo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/5/17.
 */
@Controller
@RequestMapping("user")
public class UserController {
    private UserService userService = new UserService();

    @RequestMapping(value = "toAdd",method=RequestMethod.GET)
    public String toAdd(){
        System.out.println("toAdd");
        return "add";
    }
    //当参数类型为一个引用类型的时候页面上要传递过来参数
    //页面上的参数name要保持与引用类型的成员变量一致
    //如果一致可以接收到参数如果不一致接收不到
    @RequestMapping(value = "add",method=RequestMethod.POST)
    public String add(User user){//controll参数可以写model可以写HttpServletRequest可以写HttpSession 也可以用来传参
       /* request.getParameter("id");
        request.getParameter("username");
        request.getParameter("userpassword");
        request.getParameter("sex");*/
        System.out.println("user"+user);
        System.out.println("add");
        return "redirect:list";
    }
    @RequestMapping(value = "update",method=RequestMethod.POST)
    public String update(){
        System.out.println("update");
        return "";
    }
    @RequestMapping(value = "delete",method=RequestMethod.POST)
    public String delete(){
        System.out.println("delete");
        return "";
    }
    @RequestMapping(value = "list",method= RequestMethod.GET)
    public String list(Model model){
        //controller接收页面过来的参数写到方法参数里面
        System.out.println("list");
        List<User> userList = userService.findAll();
        model.addAttribute("list",userList);
        return "list";
    }

}
